﻿Friend Class Host
    Implements Framework.Plugin.IHost

    Private Shared mMainView As FMain
    Private Shared mHost As New Host
    Private Shared mPlugs As New Generic.List(Of Framework.Plugin.IPlugIn)

    Shared Sub Init(ByVal mainView As FMain)
        mMainView = mainView
    End Sub
    Shared Sub [Stop](ByVal message As Framework.Message)
        While mPlugs.Count > 0
            mPlugs(mPlugs.Count - 1).Stop(message)
            mPlugs.RemoveAt(mPlugs.Count - 1)
        End While
    End Sub

    Public Shared Sub AddPlugin(ByVal plugin As Framework.Plugin.IPlugIn)
        mPlugs.Add(plugin)
        plugin.Start(mHost)
    End Sub

    Private Sub AddTab(ByVal tabKey As String, ByVal imageKey As String, ByVal caption As String, ByVal userControl As System.Windows.Forms.UserControl) Implements Framework.Plugin.IHost.AddTab
        mMainView.AddTab(tabKey, imageKey, caption, userControl)
    End Sub

    Public Sub AddTabImage(ByVal imageKey As String, ByVal image As System.Drawing.Image) Implements Framework.Plugin.IHost.AddTabImage
        mMainView.AddTabImage(imageKey, image)
    End Sub

    Public Sub SetTabCaption(ByVal tabKey As String, ByVal caption As String) Implements Framework.Plugin.IHost.SetTabCaption
        mMainView.SetTabCaption(tabKey, caption)
    End Sub

    Public Sub SetTabImage(ByVal tabKey As String, ByVal imageKey As String) Implements Framework.Plugin.IHost.SetTabImage
        mMainView.SetTabImage(tabKey, imageKey)
    End Sub

    Public Sub ShowTab(ByVal tabKey As String) Implements Framework.Plugin.IHost.ShowTab
        mMainView.ShowTab(tabKey)
    End Sub

    Private Sub AddMenuItem(ByVal plugin As Framework.Plugin.IPlugIn, ByVal menuKey As String, ByVal itemKey As String, ByVal text As String) Implements Framework.Plugin.IHost.AddMenuItem
        AddMenuItem(plugin, menuKey, itemKey, text, Keys.None)
    End Sub

    Private Sub AddMenuItem(ByVal plugin As Framework.Plugin.IPlugIn, ByVal menuKey As String, ByVal itemKey As String, ByVal text As String, ByVal shortCut As System.Windows.Forms.Keys) Implements Framework.Plugin.IHost.AddMenuItem
        Dim clsMI As System.Windows.Forms.ToolStripMenuItem = mPluginMenus(menuKey)
        Dim clsSMI As New System.Windows.Forms.ToolStripMenuItem(text)
        clsSMI.ShortcutKeys = shortCut
        clsMI.DropDownItems.Add(clsSMI)
        clsSMI.Name = itemKey
        mPluginByMenuItemKey.Add(itemKey, plugin)
        AddHandler clsSMI.Click, AddressOf EventHandler_MenuItemClicked
    End Sub
    Private Sub EventHandler_MenuItemClicked(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim clsMI As System.Windows.Forms.ToolStripMenuItem
            clsMI = CType(sender, System.Windows.Forms.ToolStripMenuItem)
            Dim strKey As String = clsMI.Name
            mPluginByMenuItemKey(strKey).MenuItemClicked(strKey)
        Catch ex As Exception
            Framework.Misc.HandleException(mMainView, ex)
        End Try
    End Sub

    Private mPluginByMenuItemKey As New Generic.Dictionary(Of String, Framework.Plugin.IPlugIn)
    Private mPluginMenus As New Generic.Dictionary(Of String, System.Windows.Forms.ToolStripMenuItem)
    Private Sub CreateMenu(ByVal plugin As Framework.Plugin.IPlugIn, ByVal menuKey As String, ByVal caption As String) Implements Framework.Plugin.IHost.CreateMenu
        Dim clsMI As New System.Windows.Forms.ToolStripMenuItem
        clsMI.Text = caption
        mPluginMenus.Add(menuKey, clsMI)
        mMainView.AddPlugInMenu(clsMI)
    End Sub
End Class
